package com.dao.impl;

import com.dao.ZgoodsDao;
import com.db.ConnectionFactory;
import com.entry.Zgoods;

import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class ZgoodDaoImpl implements ZgoodsDao {

    @Override
    public int add(String zname, int zcount, String gid, String sup_name) {
        int i = 0;
        Connection conn = null;
        PreparedStatement prst = null;
        String sql = "insert into zgoods(zname,zcount,gid,sup_name) values(?,?,?,?)";
        try {
            conn = ConnectionFactory.getConnection();
            prst = conn.prepareStatement(sql);
            prst.setString(1, zname);
            prst.setInt(2, zcount);
            prst.setString(3, gid);
            prst.setString(4, sup_name);
            i = prst.executeUpdate();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (prst != null)
                    prst.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public boolean findByName(String zname) {
        Connection conn = null;
        PreparedStatement prst = null;
        ResultSet rs = null;
        boolean b = false;
        String sql = "select * from zgoods where zname=?";
        try {
            conn = ConnectionFactory.getConnection();
            prst = conn.prepareStatement(sql);
            prst.setString(1, zname);
            rs = prst.executeQuery();
            if (rs.next()) {
                b = true;
            }
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (prst != null)
                    prst.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return b;
    }

    @Override
    public int update(String zname, int zcount, int oldcount, int num) {
        Connection conn = null;
        PreparedStatement prst = null;
        int i = 0;
        String sql = "update zgoods set zcount=";
        if (num == 1) {
            //增加
            int n = oldcount + zcount;
            sql += n + " where zname='" + zname + "'";
        } else if (num == 2) {
            //减少
            int n = oldcount + zcount;
            sql += n + " where zname='" + zname + "'";
        }
        try {
            conn = ConnectionFactory.getConnection();
            prst = conn.prepareStatement(sql);
            i = prst.executeUpdate();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
}
